package com.goldman.http.common.s2s.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author Bin
 * @date 2023/11/13
 */
@Slf4j
public class CustomRequestInterceptor implements HttpRequestInterceptor {
    @Override
    public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
        if (httpRequest instanceof HttpEntityEnclosingRequest) {
            HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) httpRequest;
            HttpEntity entity = entityRequest.getEntity();
            if (entity != null) {
                String originalContent = EntityUtils.toString(entity);
                String modifiedContent = modifyRequest(originalContent);
                entityRequest.setEntity(new StringEntity(modifiedContent));
            }
        }
    }

    /**
     * 修改报文体
     *
     * @param originalContent
     * @return
     */
    private String modifyRequest(String originalContent) {
        log.info("原始报文:{}", originalContent);
        return originalContent;
    }

}
